package adapter

import (
	"database/sql"
)

var (
	prepare = map[string]map[string][]string{
		"select": map[string][]string{

			"user_permission": {
				"SELECT p.num_job FROM person AS p" +
					" INNER JOIN (SELECT u.id_person FROM admin_user AS u" +
					"   INNER JOIN relation_admin_user_permission AS r ON r.id_admin_user = u.id) AS r" +
					" ON p.id = r.id_person",
				"SELECT p.name FROM admin_permission AS p" +
					" INNER JOIN relation_admin_user_permission AS r" +
					" ON p.id = r.id_admin_permission",
				"SELECT rl.level FROM relation_admin_user_permission AS rl",
			},

			"role_permission": {
				"SELECT rl.name FROM admin_role AS rl" +
					" INNER JOIN relation_admin_role_permission AS r" +
					" ON rl.id = r.id_admin_role",
				"SELECT p.name FROM admin_permission AS p" +
					" INNER JOIN relation_admin_role_permission AS r" +
					" ON p.id = r.id_admin_permission",
				"SELECT level FROM relation_admin_role_permission",
			},

			"role_user": {
				"SELECT p.num_job FROM person AS p INNER JOIN" +
					" (SELECT u.id_person FROM admin_user AS u" +
					" INNER JOIN relation_admin_user_permission AS r ON r.id_admin_user = u.id) AS r" +
					" ON p.id = r.id_person",
				"SELECT rl.name FROM admin_role AS rl" +
					" INNER JOIN relation_admin_role_permission AS r" +
					" ON rl.id = r.id_admin_role",
			},
		},

		"drop": map[string][]string{
			"user_permission": []string{"DROP TABLE IF EXISTS `workshop`.`relation_admin_user_permission`"},
			"role_permission": []string{"DROP TABLE IF EXISTS `workshop`.`relation_admin_role_permission`"},
			"role_user":       []string{"DROP TABLE IF EXISTS `workshop`.`relation_admin_role_user`"},
		},

		"create": map[string][]string{
			"user_permission": {
				"CREATE TABLE IF NOT EXISTS `workshop`.`relation_admin_user_permission` (" +
					" `id` INT NOT NULL AUTO_INCREMENT," +
					" `id_admin_user` INT NOT NULL," +
					" `id_admin_permission` INT NOT NULL," +
					" `level` VARCHAR(32) NULL DEFAULT 'read' COMMENT '权限级别(read: 读权限; write: 写权限)'," +
					" PRIMARY KEY (`id`))" +
					" ENGINE = InnoDB" +
					" AUTO_INCREMENT = 100" +
					" DEFAULT CHARACTER SET = utf8mb4" +
					" COMMENT = '权限所有者 -- 权限内容 的关联表'"},

			"role_permission": {
				"CREATE TABLE IF NOT EXISTS `workshop`.`relation_admin_role_permission` (" +
					" `id` INT NOT NULL AUTO_INCREMENT," +
					" `id_admin_role` INT NOT NULL," +
					" `id_admin_permission` INT NOT NULL," +
					" `level` VARCHAR(32) NULL DEFAULT 'read' COMMENT '权限级别(read: 读权限; write: 写权限)'," +
					" PRIMARY KEY (`id`))" +
					" ENGINE = InnoDB" +
					" AUTO_INCREMENT = 100" +
					" DEFAULT CHARACTER SET = utf8mb4" +
					" COMMENT = '角色 -- 权限 的关联表'"},

			"role_user": {
				"CREATE TABLE IF NOT EXISTS `workshop`.`relation_admin_role_user` (" +
					" `id` INT NOT NULL AUTO_INCREMENT," +
					" `id_admin_role` INT NOT NULL," +
					" `id_admin_user` INT NOT NULL," +
					" PRIMARY KEY (`id`))" +
					" ENGINE = InnoDB" +
					" AUTO_INCREMENT = 100" +
					" DEFAULT CHARACTER SET = utf8mb4" +
					" COMMENT = '角色 -- 用户 的关联表'"},
		},

		"insert": map[string][]string{
			"user_permission": {
				"INSERT INTO relation_admin_user_permission" +
					" (id_admin_user, id_admin_permission, level)" +
					" VALUES (" +
					" (SELECT u.id FROM admin_user u INNER JOIN" +
					"   (SELECT person.id FROM person WHERE person.num_job = ?) p" +
					" ON p.id = u.id_person)" +
					" , (SELECT per.id FROM admin_permission per WHERE per.name = ?)" +
					" , ?)",
			},
			"role_permission": {
				"INSERT INTO relation_admin_role_permission" +
					" (id_admin_role, id_admin_permission, level)" +
					" VALUES (" +
					" (SELECT r.id FROM admin_role r WHERE r.name = ?)" +
					" , (SELECT per.id FROM admin_permission per WHERE per.name = ?)" +
					" , ?)",
			},
			"role_user": {
				"INSERT INTO relation_admin_user_permission" +
					" (id_admin_user, id_admin_permission, level)" +
					" SELECT u.id, per.id FROM" +
					" (SELECT u.id FROM admin_user u INNER JOIN" +
					"   (SELECT person.id FROM person WHERE person.num_job = ?) p" +
					" ON p.id = u.id_person) u," +
					" (SELECT r.id FROM admin_role r WHERE r.name = ?) r",
			},
		},

		"update": map[string][]string{
			"user_permission": {
				"UPDATE relation_admin_user_permission SET level = ? WHERE id = ?",
			},
			"role_permission": {
				"UPDATE relation_admin_role_permission SET level = ? WHERE id = ?",
			},
		},
		"delete": map[string][]string{
			"user_permission": {
				"DELETE FROM relation_admin_user_permission" +
					" WHERE id_admin_user IN" +
					" (SELECT u.id FROM admin_user u INNER JOIN" +
					"   (SELECT person.id FROM person WHERE person.num_job = ?)" +
					" ON p.id = u.id_person) " +
					" AND id_admin_permission IN (SELECT per.id FROM admin_permission per WHERE per.name = ?)" +
					" AND level = ?",
			},
			"role_permission": {
				"DELETE FROM relation_admin_role_permission" +
					"WHERE id_admin_role IN (SELECT r.id FROM admin_role r WHERE r.name = ?)" +
					" AND id_admin_permission IN (SELECT per.id FROM admin_permission per WHERE per.name = ?)" +
					" AND level = ?",
			},
			"role_user": {
				"DELETE FROM relation_admin_user_permission" +
					" WHERE id_admin_user IN" +
					" (SELECT u.id FROM admin_user u INNER JOIN" +
					"   (SELECT person.id FROM person WHERE person.num_job = ?) p" +
					" ON p.id = u.id_person)" +
					" AND id_admin_role IN (SELECT r.id FROM admin_role r WHERE r.name = ?)",
			},
		},
	}
)

func initPermission(rule map[string]map[string][]string, db *sql.DB) (stmts map[string]map[string][]*sql.Stmt, err error) {
	stmts = map[string]map[string][]*sql.Stmt{}
	for k, v := range rule {
		stmts[k] = map[string][]*sql.Stmt{}
		for sk, sv := range v {
			stmts[k][sk] = []*sql.Stmt{}
			for _, ssv := range sv {
				stmt, err := db.Prepare(ssv)
				if err != nil {
					return nil, err
				}
				stmts[k][sk] = append(stmts[k][sk], stmt)
			}
		}
	}
	return stmts, nil
}
